package com.ai.service.made.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ai.check.CameraCheck;

import com.ai.common.CommonResponse;
import com.ai.common.constant.*;
import com.ai.entity.cameraFunctions.FullFunction;
import com.ai.entity.cameraFunctions.functions.CameraFunctionBindBO;
import com.ai.entity.cameraFunctions.functions.CameraFunctionsPO;
import com.ai.entity.cameraFunctions.functions.FunctionBasicsVO;
import com.ai.entity.cameraFunctions.timeInterval.TimeIntervalPO;
import com.ai.entity.cameraFunctions.timeInterval.TimeIntervalVO;
import com.ai.entity.function.FunctionPO;
import com.ai.entity.made.*;
import com.ai.entity.made.camera.CameraPO;
import com.ai.entity.made.camera.CameraVO;
import com.ai.entity.made.camera.SwitchCameraAlgorithmBO;
import com.ai.entity.made.camera.SwitchCameraFunctionAlgorithmBO;
import com.ai.mapper.made.CameraFunctionsMapper;
import com.ai.networking.netty.handler.ServerHandler;
import com.ai.service.made.*;
import com.ai.util.SecurityUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Mr.Chen
 * @since 2020-11-15
 */
@Slf4j
@Service
public class CameraFunctionsServiceImpl extends ServiceImpl<CameraFunctionsMapper, CameraFunctionsPO> implements CameraFunctionsService {
    @Resource
    RegionsService regionsService;

    @Resource
    TimeIntervalService timeIntervalService;

    @Resource
    CameraService cameraService;
    @Resource
    FunctionService functionService;
    @Resource
    private CameraFunctionsMapper cameraFunctionsMapper;
    @Resource
    private CameraFunctionsService cameraFunctionsService;


    @Resource
    private AlgorithmCameraService algorithmCameraService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;


    @Resource
    private ICoalRoutingInspectionService iCoalRoutingInspectionService;

    @Resource
    private ICoalOutOffStationParameterService iCoalOutOffStationParameterService;


    @Resource
    private ICoalCraneInspectionService iCoalCraneInspectionService;

    @Resource
    private IModbusNodeInfoService iModbusNodeInfoService;

    @Resource
    private IModbusServerInfoService iModbusServerInfoService;

    @Resource
    private IOpcNodeInfoService iOpcNodeInfoService;

    @Resource
    private IOpcServerInfoService iOpcServerInfoService;

    @Override
    public void sendFullFunctions() {
        List<FullFunction> fullFunctionList = getFullFunctions();
        String KEY_ID = "ai";
        ServerHandler.sendMessage(KEY_ID, JSON.toJSONString(fullFunctionList));
    }

    @Override
    public List<FullFunction> getFullFunctions() {

        List<FullFunction> fullFunctionList = new ArrayList<>();

        List<FunctionPO> functionPOList = functionService.list();
        for (FunctionPO functionPO : functionPOList) {
            log.info("functionPO = " + JSON.toJSONString(functionPO));
            FullFunction fullFunction = new FullFunction();
            fullFunction.setType(functionPO.getType());
            fullFunction.setCameraList(getCameraByType(functionPO));
            fullFunctionList.add(fullFunction);
        }

        log.info("fullFunctionList = " + fullFunctionList);
        return fullFunctionList;
    }

    /**
     * 根据功能获取对应的所有摄像头信息，不包含摄像头的直播状态与功能状态
     *
     * @param functionPO
     * @return
     */
    @Override
    public List<SwitchCameraAlgorithmBO> getCameraByType(FunctionPO functionPO) {
        return getCameraByType(functionPO, null, null);
    }

    @Override
    public List<SwitchCameraAlgorithmBO> getCameraByType(FunctionPO functionPO, Integer status, Map<String, Object> extendedProperties) {

        int functionId = functionPO.getId();
        int type = functionPO.getType();

        List<SwitchCameraAlgorithmBO> switchCameraAlgorithmBOList = new ArrayList<>();

        List<CameraFunctionsPO> cameraFunctionsPOList = list(new QueryWrapper<CameraFunctionsPO>().eq("function_id", functionId));
        for (CameraFunctionsPO cameraFunctionsPO : cameraFunctionsPOList) {
            SwitchCameraAlgorithmBO switchCameraAlgorithmBO = new SwitchCameraAlgorithmBO();

            CameraPO cameraPO = cameraService.getById(cameraFunctionsPO.getCameraId());
            switchCameraAlgorithmBO.setCameraId(cameraPO.getId())
                    .setCameraCode(cameraPO.getCode())
                    .setUsername(cameraPO.getUsername())
                    .setPassword(cameraPO.getPassword())
                    .setIp(cameraPO.getIp())
                    .setUrl(cameraPO.getUrl()).setPort(cameraPO.getPort()).setStreamWay(cameraPO.getStreamWay());

            if (status != null) {
                switchCameraAlgorithmBO.setState(status);
            } // 操作状态
            if (extendedProperties != null) {
                switchCameraAlgorithmBO.setExtendedProperties(extendedProperties);// 附加属性
            }

            List<SwitchCameraFunctionAlgorithmBO> functionList = new ArrayList<>();
            SwitchCameraFunctionAlgorithmBO functionAlgorithmBO = new SwitchCameraFunctionAlgorithmBO();
            functionAlgorithmBO.setId(cameraFunctionsPO.getId()).setType(type + "")
                    .setThreshold(cameraFunctionsPO.getThreshold())
                    .setSpecialOne(cameraFunctionsPO.getSpecialOne())
                    .setSpecialThree(cameraFunctionsPO.getSpecialTwo())
                    .setSpecialThree(cameraFunctionsPO.getSpecialThree())
                    .setSpecialFour(cameraFunctionsPO.getSpecialFour())
                    .setSpecialFive(cameraFunctionsPO.getSpecialFive())
                    .setTimeIntervals(timeIntervalService.getTimeIntervalList(cameraFunctionsPO.getId()))
                    .setSpots(regionsService.getSpots(cameraFunctionsPO.getId()));
            functionList.add(functionAlgorithmBO);

            switchCameraAlgorithmBO.setFunctions(functionList);
            switchCameraAlgorithmBOList.add(switchCameraAlgorithmBO);
        }
        return switchCameraAlgorithmBOList;
    }

    @Override
    public CommonResponse cameraFunctionList(BaseIdOrNameBO baseIdOrNameBO) {
        CameraCheck cameraCheck = new CameraCheck();
        if (cameraCheck.isCheckID(baseIdOrNameBO.getCurrentId(), baseIdOrNameBO.getId())) {
            return cameraCheck.resultData;
        } else {
            int cameraId = baseIdOrNameBO.getId();
            String nickname = baseIdOrNameBO.getNickname();
            if (StringUtils.isEmpty(nickname)) {
                nickname = "%";
            } else {
                nickname = "%" + nickname + "%";
            }

            Map<String, List<FunctionBasicsVO>> map = new HashMap();
            List<FunctionBasicsVO> uncheckedList = this.cameraFunctionsMapper.unchecked(cameraId, nickname);
            List<FunctionBasicsVO> checkedList = this.cameraFunctionsMapper.checked(cameraId, nickname);
            map.put("unchecked", uncheckedList);
            map.put("checked", checkedList);
            return CommonResponse.ok(map);
        }
    }

    @Override
    public CommonResponse checked(CameraFunctionBindBO cameraFunctionBindBO) {
        CameraCheck cameraCheck = new CameraCheck();
        if (cameraCheck.isCheckID(cameraFunctionBindBO.getCurrentId(), cameraFunctionBindBO.getCameraId())) {
            return cameraCheck.resultData;
        } else {
            int cameraId = cameraFunctionBindBO.getCameraId();
            List<Integer> functionIds = cameraFunctionBindBO.getFunctionIds();
            if (functionIds != null && functionIds.size() > 0) {
                List<CameraFunctionsPO> cameraFunctionsPOList = new ArrayList();
                Iterator var6 = functionIds.iterator();

                while (var6.hasNext()) {
                    int id = (Integer) var6.next();
                    CameraFunctionsPO cameraFunctionsPO = new CameraFunctionsPO();
                    cameraFunctionsPO.setFunctionId(id);
                    cameraFunctionsPO.setCameraId(cameraId);
                    cameraFunctionsPOList.add(cameraFunctionsPO);
                }


                boolean saveBatchBool = this.saveBatch(cameraFunctionsPOList) ;

                if (saveBatchBool) {
                    //更新缓存
                    CameraPO cameraPO = cameraService.getById(cameraId);
                    SwitchCameraAlgorithmBO switchCameraAlgorithmBO = algorithmCameraService.getSwitchCameraAlgorithmBO(cameraPO);
                    stringRedisTemplate.opsForValue().set(RedisConstants.CAMERA_FUN_INFO+cameraId, JSONObject.toJSONString(switchCameraAlgorithmBO));
                    return CommonResponse.ok("提交成功");
                }else {
                    return CommonResponse.error("提交失败");
                }
            } else {
                return CommonResponse.error("请选择需要绑定的功能");
            }
        }
    }

    @Override
    public CommonResponse unchecked(CameraFunctionBindBO cameraFunctionBindBO) {
        CameraCheck cameraCheck = new CameraCheck();
        if (cameraCheck.isCheckID(cameraFunctionBindBO.getCurrentId(), cameraFunctionBindBO.getCameraId())) {
            // : 2023-06-07 通知算法，待完成
//            cameraFunctionsService.sendFullFunctions();

            return cameraCheck.resultData;
        } else {
            List<Integer> functionIds = cameraFunctionBindBO.getFunctionIds();
            if (functionIds != null && functionIds.size() > 0) {
//                cameraFunctionsService.sendFullFunctions();

                return this.removeByIds(functionIds) ? CommonResponse.ok("提交成功") : CommonResponse.error("提交失败");
            } else {
                // : 2023-06-07 通知算法，待完成
//                cameraFunctionsService.sendFullFunctions();

                return CommonResponse.error("请选择需要取消的功能");
            }
        }
    }

    @Override
    public CommonResponse config(BaseIdOrNameBO baseIdOrNameBO) {
        if (baseIdOrNameBO.getId() == null) {
            return CommonResponse.error("请选择功能");
        } else {
            CameraFunctionsPO cameraFunctionsPO = (CameraFunctionsPO) this.getById(baseIdOrNameBO.getId());
            if (cameraFunctionsPO == null) {
                return CommonResponse.error("请选择功能");
            } else {
                TimeIntervalVO timeIntervalVO = new TimeIntervalVO();
                BeanUtils.copyProperties(cameraFunctionsPO, timeIntervalVO);
                List<TimeIntervalPO> timeList = this.timeIntervalService.list((Wrapper) (new QueryWrapper()).eq("camera_functions_id", baseIdOrNameBO.getId()));
                if (timeList.size() <= 0) {
                    TimeIntervalPO po = new TimeIntervalPO();
                    po.setId(0);
                    po.setCameraFunctionsId(baseIdOrNameBO.getId());
                    po.setStartTime("00:00:00");
                    po.setEndTime("23:59:59");
                    timeList.add(po);
                }
                FunctionPO functionPO = functionService.getById(cameraFunctionsPO.getFunctionId());
                if (FunctionTypeConstants.INSPECTION_TRAJECTORY.equals(String.valueOf(functionPO.getType()))){

                    //获取巡检相关的参数
                    List<CoalRoutingInspection> routingInspectionList =
                            iCoalRoutingInspectionService.list(new QueryWrapper<CoalRoutingInspection>().eq("camera_function_id", cameraFunctionsPO.getId()));

                    timeIntervalVO.setInspectionList(routingInspectionList);
                }

                if (FunctionTypeConstants.REGULAR_POST_OFF.equals(String.valueOf(functionPO.getType()))){
                    List<CoalOutOffStationParameter> coalOutOffStationParameters = iCoalOutOffStationParameterService.list(new QueryWrapper<CoalOutOffStationParameter>()
                            .eq("camera_function_id", cameraFunctionsPO.getId())
                            .eq("function_type", FunctionTypeConstants.REGULAR_POST_OFF));
                    timeIntervalVO.setCoalOutOffStationParameters(coalOutOffStationParameters);
                }

                if (FunctionTypeConstants.MONORAIL_STATION_DETECTION.equals(String.valueOf(functionPO.getType()))){
                    List<CoalCraneInspection> coalCraneInspectionList = iCoalCraneInspectionService.list(new QueryWrapper<CoalCraneInspection>()
                            .eq("camera_function_id", cameraFunctionsPO.getId())
                            .eq("function_type", FunctionTypeConstants.MONORAIL_STATION_DETECTION));
                    timeIntervalVO.setCoalCraneInspections(coalCraneInspectionList);
                }

                if(org.apache.commons.lang3.StringUtils.isNotBlank(cameraFunctionsPO.getOpcNodeId())){
                    //不为空
                    OpcNodeInfo opcNodeInfo = iOpcNodeInfoService.getOne(new QueryWrapper<OpcNodeInfo>()
                            .eq("id", cameraFunctionsPO.getOpcNodeId()));
                    CameraPO cameraPO = cameraService.getByCode(cameraFunctionsPO.getOpcNodeId());
                    ModbusNodeInfo modbusNodeInfo = iModbusNodeInfoService.getById(cameraFunctionsPO.getOpcNodeId());

                    if (!Objects.isNull(opcNodeInfo)){
                        OpcServerInfo opcServerInfo = iOpcServerInfoService.getOne(new QueryWrapper<OpcServerInfo>().eq("id", opcNodeInfo.getOpcServerInfoId()));
                        timeIntervalVO.setOpcNodeId(opcNodeInfo.getId());
                        timeIntervalVO.setOpcServerId(opcServerInfo.getId());
                        timeIntervalVO.setDeviceType(OpcConstant.DEVICE_TYPE);
                    } else if (ObjectUtil.isNotNull(cameraPO)) {
                        timeIntervalVO.setOpcNodeId(cameraPO.getCode());
                        //信号摄像头默认id为3
                        timeIntervalVO.setOpcServerId(Constants.DEVICE_TYPE_SIGNAL_CAMERA_DEFAULT_ID);
                        timeIntervalVO.setDeviceType(Constants.DEVICE_TYPE_SIGNAL_CAMERA);
                    } else if (ObjectUtil.isNotEmpty(modbusNodeInfo)) {
                        ModbusServerInfo modbusServerInfo = iModbusServerInfoService.getById(modbusNodeInfo.getModbusServerId());
                        timeIntervalVO.setOpcNodeId(modbusNodeInfo.getId());
                        timeIntervalVO.setDeviceType(ModbusConstants.DEVICE_TYPE);
                        if (ObjectUtil.isNotEmpty(modbusServerInfo)){
                            timeIntervalVO.setOpcServerId(modbusServerInfo.getId());
                        }
                    }
                }

                timeIntervalVO.setTimeList(timeList);

                return CommonResponse.ok(timeIntervalVO);
            }
        }
    }

    @Override
    public CommonResponse updateConfig(TimeIntervalVO timeIntervalVO) {
        Integer id = timeIntervalVO.getId();
        if (id != null && id != 0) {
            CameraFunctionsPO cameraFunctionsPO = new CameraFunctionsPO();
            BeanUtils.copyProperties(timeIntervalVO, cameraFunctionsPO);
            if (!this.updateById(cameraFunctionsPO)) {
                //基础信息配置
                return CommonResponse.error("修改失败");
            } else {
                //更新相关参数
                List<TimeIntervalPO> timeList = timeIntervalVO.getTimeList();

                List<CoalRoutingInspection> inspectionList = timeIntervalVO.getInspectionList();

                List<CoalOutOffStationParameter> coalOutOffStationParameters = timeIntervalVO.getCoalOutOffStationParameters();

                List<CoalCraneInspection> coalCraneInspections = timeIntervalVO.getCoalCraneInspections();

                CameraFunctionsPO dbCameraFunction = this.getById(id);
                Integer functionId = dbCameraFunction.getFunctionId();
                FunctionPO functionPO = functionService.getById(functionId);
                String type = String.valueOf(functionPO.getType());
                //巡检参数填充
                if (type.equals(FunctionTypeConstants.INSPECTION_TRAJECTORY) ){
                    //准备新增巡检机制的相关参数
                    iCoalRoutingInspectionService.remove(new QueryWrapper<CoalRoutingInspection>().eq("camera_function_id",id));
                    if (CollectionUtil.isNotEmpty(inspectionList)){
                        //移除该功能原本对应的所有参数关系
                        LinkedList<CoalRoutingInspection> coalRoutingInspectionLinkedList = new LinkedList<>();
                        for (CoalRoutingInspection coalRoutingInspection : inspectionList) {
                            coalRoutingInspection.setCameraFunctionId(String.valueOf(id));
                            coalRoutingInspection.setUpdateTime(LocalDateTime.now());
                            coalRoutingInspection.setCreateTime(LocalDateTime.now());
                            coalRoutingInspection.setCreateUser(SecurityUtils.getLoginUserName());
                            coalRoutingInspection.setUpdateUser(SecurityUtils.getLoginUserName());
                            coalRoutingInspectionLinkedList.add(coalRoutingInspection);
                        }
                        iCoalRoutingInspectionService.saveBatch(coalRoutingInspectionLinkedList);
                    }else {
                        //页面没有配置，设置一个默认的配置参数
                        CoalRoutingInspection defaultCoalRoutingInspection = new CoalRoutingInspection();
                        defaultCoalRoutingInspection.setCameraFunctionId(String.valueOf(id));
                        //默认巡检时间段及次数
                        defaultCoalRoutingInspection.setStartTime("00:00:00");
                        defaultCoalRoutingInspection.setEndTime("23:59:59");
                        defaultCoalRoutingInspection.setDetectionNum(3);
                        defaultCoalRoutingInspection.setUpdateTime(LocalDateTime.now());
                        defaultCoalRoutingInspection.setCreateTime(LocalDateTime.now());
                        defaultCoalRoutingInspection.setCreateUser(SecurityUtils.getLoginUserName());
                        defaultCoalRoutingInspection.setUpdateUser(SecurityUtils.getLoginUserName());
                        iCoalRoutingInspectionService.save(defaultCoalRoutingInspection);
                    }
                }

                if (type.equals(FunctionTypeConstants.MONORAIL_STATION_DETECTION)){
                    iCoalCraneInspectionService.remove(new QueryWrapper<CoalCraneInspection>().eq("camera_function_id",id));
                    if (CollectionUtil.isNotEmpty(coalCraneInspections)){
                        LinkedList<CoalCraneInspection> craneInspectionLinkedList = new LinkedList<>();
                        for (CoalCraneInspection coalCraneInspection : coalCraneInspections) {
                            coalCraneInspection.setCameraFunctionId(String.valueOf(id));
                            coalCraneInspection.setFunctionType(type);
                            coalCraneInspection.setCreateTime(LocalDateTime.now());
                            coalCraneInspection.setUpdateTime(LocalDateTime.now());
                            coalCraneInspection.setUpdateUser(SecurityUtils.getLoginUserName());
                            coalCraneInspection.setCreateUser(SecurityUtils.getLoginUserName());
                            craneInspectionLinkedList.add(coalCraneInspection);
                        }
                        iCoalCraneInspectionService.saveBatch(craneInspectionLinkedList);
                    }else {
                        CoalCraneInspection coalCraneInspection = new CoalCraneInspection();
                        coalCraneInspection.setCameraFunctionId(String.valueOf(id));
                        coalCraneInspection.setFunctionType(type);
                        coalCraneInspection.setBeginTime("00:00:00");
                        coalCraneInspection.setEndTime("23:59:59");
                        coalCraneInspection.setAccumulatedMaintenanceTime("180");
                        coalCraneInspection.setSingleMaintenanceDuration("20");

                        coalCraneInspection.setCreateTime(LocalDateTime.now());
                        coalCraneInspection.setUpdateTime(LocalDateTime.now());
                        coalCraneInspection.setUpdateUser(SecurityUtils.getLoginUserName());
                        coalCraneInspection.setCreateUser(SecurityUtils.getLoginUserName());

                        iCoalCraneInspectionService.save(coalCraneInspection);
                    }
                }

                if (type.equals(FunctionTypeConstants.REGULAR_POST_OFF)){
                    //首先移除原本所有的关系
                    iCoalOutOffStationParameterService.remove(new QueryWrapper<CoalOutOffStationParameter>().eq("camera_function_id",id));
                    if (CollectionUtil.isNotEmpty(coalOutOffStationParameters)){
                        LinkedList<CoalOutOffStationParameter> offStationParameterLinkedList = new LinkedList<>();
                        for (CoalOutOffStationParameter coalOutOffStationParameter : coalOutOffStationParameters) {
                            coalOutOffStationParameter.setCameraFunctionId(String.valueOf(id));
                            coalOutOffStationParameter.setFunctionType(type);
                            coalOutOffStationParameter.setCreateTime(LocalDateTime.now());
                            coalOutOffStationParameter.setUpdateTime(LocalDateTime.now());
                            coalOutOffStationParameter.setCreateUser(SecurityUtils.getLoginUserName());
                            coalOutOffStationParameter.setUpdateUser(SecurityUtils.getLoginUserName());
                            offStationParameterLinkedList.add(coalOutOffStationParameter);
                        }
                        iCoalOutOffStationParameterService.saveBatch(offStationParameterLinkedList);
                    }else {
                        //页面没有配置，设置一个默认的参数
                        CoalOutOffStationParameter coalOutOffStationParameter = new CoalOutOffStationParameter();
                        coalOutOffStationParameter.setCameraFunctionId(String.valueOf(id));
                        coalOutOffStationParameter.setFunctionType(type);
                        //默认岗位人数三个人，限时脱岗时间10分钟
                        coalOutOffStationParameter.setPeopleNum(3);
                        coalOutOffStationParameter.setOutOffStationTime("10");
                        coalOutOffStationParameter.setOutOffStationTime("00:00:00");
                        coalOutOffStationParameter.setEndTime("23:59:59");

                        coalOutOffStationParameter.setCreateTime(LocalDateTime.now());
                        coalOutOffStationParameter.setUpdateTime(LocalDateTime.now());
                        coalOutOffStationParameter.setCreateUser(SecurityUtils.getLoginUserName());
                        coalOutOffStationParameter.setUpdateUser(SecurityUtils.getLoginUserName());

                        iCoalOutOffStationParameterService.save(coalOutOffStationParameter);
                    }



                }

                this.timeIntervalService.remove((Wrapper) (new QueryWrapper()).eq("camera_functions_id", id));
                if (timeList != null && timeList.size() > 0) {
                    timeList.forEach((timeIntervalPOx) -> {
                        timeIntervalPOx.setCameraFunctionsId(id);
                        this.timeIntervalService.save(timeIntervalPOx);
                    });
                } else {
                    TimeIntervalPO timeIntervalPO = new TimeIntervalPO();
                    timeIntervalPO.setCameraFunctionsId(timeIntervalVO.getId());
                    timeIntervalPO.setStartTime("00:00:00");
                    timeIntervalPO.setEndTime("23:59:59");
                    this.timeIntervalService.save(timeIntervalPO);
                }


                //准备更新指定摄像头的缓存参数
                log.info("===========================  准备更新摄像头信息  ===========================");
                Integer cameraId = dbCameraFunction.getCameraId();
                CameraPO cameraPO = cameraService.getById(cameraId);
                LinkedList<CameraPO> cameraPOLinkedList = new LinkedList<>();
                cameraPOLinkedList.add(cameraPO);
                algorithmCameraService.cacheAppointCameraList(cameraPOLinkedList);

                // : 2023-06-07 通知算法，待完成
//                cameraFunctionsService.sendFullFunctions();
                return CommonResponse.ok("修改成功");
            }
        } else {

            // : 2023-06-07 通知算法，待完成
//            cameraFunctionsService.sendFullFunctions();

            return CommonResponse.error("请选择功能");
        }
    }

    @Override
    public CommonResponse listCameraByFunction(BaseIdOrNameBO baseIdOrNameBO) {
        if (baseIdOrNameBO.getId() == null) {
            return CommonResponse.error("请选择功能");
        } else {
            List<CameraVO> cameraVOList = this.cameraFunctionsMapper.listCameraByFunction(baseIdOrNameBO.getId());
            return CommonResponse.ok(cameraVOList);
        }
    }

    @Override
    public List<CameraPO> getCameraSteelList(ArrayList<Integer> funTypeList) {
        return    baseMapper.getCameraSteelList(funTypeList);
    }

    @Override
    public List<CameraFunctionsPO> getBindFunctions(int cameraId) {

        QueryWrapper<CameraFunctionsPO> queryWrapper = new QueryWrapper<CameraFunctionsPO>().eq("camera_id", cameraId);

        return this.list(queryWrapper);

    }

    @Override
    public List<CameraPO> getCameraPoListByFunctionType(Integer functionId) {

        QueryWrapper<CameraFunctionsPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("function_id", functionId);
        List<CameraFunctionsPO> cameraFunctionsPOList = cameraFunctionsService.list(queryWrapper);
        if (CollectionUtils.isEmpty(cameraFunctionsPOList)){
            return null;
        }
        List<Integer> cameraIds = cameraFunctionsPOList.stream()
                .map(cameraFunctionsPO -> cameraFunctionsPO.getCameraId()).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(cameraIds)) {
            return cameraService.getCameraListByIds(cameraIds);
        }else {
            return null;
        }
    }

    @Override
    public String getRelationByCameraIdFunction(String cameraId, String functionType) {

        //根据功能type值获取功能id
        FunctionPO functionPo = functionService.getOne(new QueryWrapper<FunctionPO>().eq("type", functionType));
        CameraFunctionsPO cameraFunctionsPO = this.getOne(new QueryWrapper<CameraFunctionsPO>().eq("camera_id", cameraId).eq("function_id", functionPo.getId()));

        if (Objects.isNull(cameraFunctionsPO)){
            return null;
        }
        return String.valueOf(cameraFunctionsPO.getId());
    }


}


